var logistRecordCtrl  = new XFApp.client();
$.views.converters({
	subString : function(val){
		return logistRecordCtrl.subString(val,7);
	},
	
	ftime : function (val){		
		return logistRecordCtrl.formateTime(val);
	},

    formatDate : function(val){
    	var date = new Date(val);
    	return date.format("yyyy-MM-dd hh:mm:ss");
    },
	
	fauditStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in auditStates){
			var auditState = auditStates[index];
			if(auditState.dictCode == value){
				return auditState.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	
});


XFApp.client.prototype.logistRecord = {
		successCode : "10000",	
		logistRecordPage : null,
		checkVal :"",
		showId : null,
		urlItems : {
			initListURL: basePath+"/logistRecord/init",
			platformApproveURL : basePath+"/logistRecord/platfromApprove",
			approveURL : basePath+"/logistRecord/approve",
			ciqApproveURL : basePath+"/logistRecord/ciqApprove",
			showDetailURL:basePath+"/logistRecord/showDetail",
		},	
		
		fauditStatus : function(value) {
			if(!value){
				return "";
			}
			for(index in auditStates){
				var auditState = auditStates[index];
				if(auditState.dictCode == value){
					return auditState.dictName;
				}
			}
			return  "未知类型("+value+")";
		},
		
		//显示详细
		showDetail : function(id){
			_this = this;
			_this.showId = id ;
			_this.detail(id);			
		},
		
		detail : function (id){			
			_this = this;		
			var params = {					
					id : id 
			};
			
			$.ajax({
				url :_this.urlItems.showDetailURL,
				data : params,
				dataType : "json",
				type : 'post',
				success : function(data) {
					if(data.code == _this.successCode){						
						$("#logist_modal").modal();
						$("#dt_customerName").text(data.result.customerName);
						$("#dt_logisticsCode").text(data.result.logisticsCode);
						$("#dt_logisticsName").text(data.result.logisticsName);
						$("#dt_appUName").text(data.result.appUName);
						$("#dt_appUId").text(data.result.appUId);
						$("#dt_remark").text(data.result.remark);						
						$("#dt_auditState").text(_this.fauditStatus(data.result.auditState));
		        	}else{		        		
		        		window.webDialogUtil.alert("提示",data.msg);
		        	}
				}
			});			
		},
		
		
		
		load : function (){
			
            _this = this;
			
			if(!!_this.logistRecordPage){
				_this.logistRecordPage.page("remote",0,$('#searchForm').serialize());
			}else{
				_this.logistRecordPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.initListURL,
			        params: $('#searchForm').serialize(), 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		if(null == data 
									|| null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1){
			        			$("#data_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}else{
			        			$("#data_grid").html($("#dataTmp").render(data.result.itemList));
			        		}
			        		
			        	}else{
			        		window.webDialogUtil.alert("提示","数据查询失败!");			        		  
			        	}
			        },
			        error : function(){
			        	window.webDialogUtil.alert("提示","数据查询失败!");
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		
		//海关审批
		approve : function (option,type){
			
            _this = this;
            
            if(!_this.checkVal){
            	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");            	
            	return ;
            }
            
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.approveConfirm(option);
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
             });
		},          
            
          
		approveConfirm: function(option){
    		    	
    		    	var index = _this.checkVal.lastIndexOf(",");            
    	            var id = _this.checkVal.substring(0,index);
    	            var params = {	
    	            		op  : option, //1-审批通过 2-审批拒绝
    	            		ids : id 				
    				}
    	            
    	            var showText ;    	            
    	            if(option == '1')
    	            	showText = "审批通过" ;
    	            else
    	            	showText = "审批拒绝" ;
    		    	
    		    	$.ajax({
    		    		url :_this.urlItems.approveURL,
    		    		data : params,
    		    		dataType : "json",
    		    		type : 'post',
    		    		success : function(data) {				
    		    			if(data.code==_this.successCode){ 
    		    				window.webDialogUtil.alert("提示",data.msg);    					
    		    				$("[name='chkItem']:checked").each(function (){
    		    					$("#custmAprvSts"+$(this).val()).text(showText);
    		    				})
    					
    		    				//移除事件
    		    				$("#checkAll").removeAttr("checked");
    		    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
    					
    					    	        		
    		    			}else{
    		    				window.webDialogUtil.alert("提示",data.msg);
    		    			}
    		    		}
    		    	});
		},
    	  
		
		// 检验检疫审批
		ciqApprove : function (option,type){
			
            _this = this;
            
            if(!_this.checkVal){
            	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");            	
            	return ;
            }
            
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.ciqApproveConfirm(option);
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
             });
		},          
            
          
		ciqApproveConfirm: function(option){
    		    	
    		    	var index = _this.checkVal.lastIndexOf(",");            
    	            var id = _this.checkVal.substring(0,index);
    	            var params = {	
    	            		op  : option, //1-审批通过 2-审批拒绝
    	            		ids : id 				
    				}
    	            
    	            var showText ;    	            
    	            if(option == '1')
    	            	showText = "审批通过" ;
    	            else
    	            	showText = "审批拒绝" ;
    		    	
    		    	$.ajax({
    		    		url :_this.urlItems.ciqApproveURL,
    		    		data : params,
    		    		dataType : "json",
    		    		type : 'post',
    		    		success : function(data) {				
    		    			if(data.code==_this.successCode){ 
    		    				window.webDialogUtil.alert("提示",data.msg);    					
    		    				$("[name='chkItem']:checked").each(function (){
    		    					$("#ciqAprvSts"+$(this).val()).text(showText);
    		    				})
    					
    		    				//移除事件
    		    				$("#checkAll").removeAttr("checked");
    		    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
    					
    					    	        		
    		    			}else{
    		    				window.webDialogUtil.alert("提示",data.msg);
    		    			}
    		    		}
    		    	});
		},
		
		//平台审批
		platformApprove : function (option,type){
			
            _this = this;
            
            if(!_this.checkVal){
            	window.webDialogUtil.alert("提示","请选择一条记录进行操作!!");
            	return ;
            }
            
            
            webAlert({
                title:'提示',
                content:'确认执行'+type+'操作吗?',
                opacity:.5,
                bg:"gray",
                width:300,
                height:10,
                button:[
                        {
                        name:"确定",
                        callback:function(){
                        	_this.paltfromApproveConfirm(option);
                        	this.close();
                        }
                        },{
                        name:"取消",
                        callback:function(){
                      	  this.close();
                        }
                        }]
             });
		},          
            
          
		paltfromApproveConfirm: function(option){
    		    	
    		    	var index = _this.checkVal.lastIndexOf(",");            
    	            var id = _this.checkVal.substring(0,index);
    	            var params = {	
    	            		op  : option, //1-审批通过 2-审批拒绝
    	            		ids : id 				
    				}
    	            
    	            var showText ;    	            
    	            if(option == '1')
    	            	showText = "审批通过" ;
    	            else
    	            	showText = "审批拒绝" ;
    		    	
    		    	$.ajax({
    		    		url :_this.urlItems.platformApproveURL,
    		    		data : params,
    		    		dataType : "json",
    		    		type : 'post',
    		    		success : function(data) {				
    		    			if(data.code==_this.successCode){    		    				
    		    				window.webDialogUtil.alert("提示",data.msg);    					
    		    				$("[name='chkItem']:checked").each(function (){
    		    					$("#approveStatus"+$(this).val()).text(showText);
    		    				})
    					
    		    				//移除事件
    		    				$("#checkAll").removeAttr("checked");
    		    				$("[name='chkItem']:checkbox").removeAttr("checked");//取消全选
    					
    					    	        		
    		    			}else{
    		    				window.webDialogUtil.alert("提示",data.msg);
    		    			}
    		    		}
    		    	});
		},
		
		
		event : function(){
			_this = this;
			
			$("#btnSearch").on("click",function(){
				_this.load();
			});
			
			$("#btnReset").on("click",function(){				
				
				$("#customerName").val("");
				$("#logisticsCode").val("");
				$("#logisticsName").val("");			
				$("#reqTimeStart").val("");
				$("#reqTimeEnd").val("");	
				// 3:All 查詢所有
				$("#approveState").val(3);
				
			});
			//海关审批
			$("#btnPass").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.approve(1,$("#btnPass").text());
			});
			
			//海关审批 - 拒絕
			$("#btnRefuse").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.approve(2,$("#btnRefuse").text());
			});
			
			//检验检疫审批
			$("#ciqAprvBtn").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.ciqApprove(1,$("#ciqAprvBtn").text());
			});
			
			//检验检疫审批- 拒絕
			$("#ciqRefuseBtn").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.ciqApprove(2,$("#ciqRefuseBtn").text());
			});
			
			//平台审批
			$("#approveBtn").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.platformApprove(1,$("#approveBtn").text());
			});	
			
			//平台审批 - 拒絕
			$("#approveRefuseBtn").on("click",function(){
				var chkVal = "";
				$("#data_grid input[type='checkbox']").each(function(){
					if($(this).prop("checked")){
						chkVal += $(this).val() +",";
					}
				});		
				
				_this.checkVal = chkVal ;				
				_this.platformApprove(2,$("#approveRefuseBtn").text());
			});
			
			$("#xClose").on("click",function(){
				$("#logist_modal").hide();
			});
			$("#btnClose").on("click",function(){
				$("#logist_modal").hide();
			});
			
			$("#checkAll").bind("click",function(){				
			   var chkVal = ""; 
			   //全选
			   if($("#checkAll").prop("checked")){
				   $("[name='chkItem']").prop("checked","checked");
				   $("[name='chkItem']:checkbox:checked").each(function (){
					   chkVal += $(this).val()+",";
				   })
				   
			   }else{
				   $("[name='chkItem']").removeAttr("checked");//取消全选
				   chkVal = "";
			   }	
			   _this.checkVal = chkVal;
			});	
			
		},
		
		
};


$(function(){
	logistRecordCtrl.logistRecord.load();
	logistRecordCtrl.logistRecord.event();
});